const { log } = require("console");
const http = require("http");
const server = http.createServer((request, response) => {
   
    //获取请求方式  GET / POST
    console.log("method", request.method);

    //获取所有的请求头对象
    console.log("headers", request.headers);
    
    //获取请求地址, 不带基础地址,带query参数
    console.log("url", request.url);// /login?username=a&pwd=b
    
    //获取请求参数
    //query参数  request.url:/login?username=a&password=b
    const { pathname, searchParams } = new URL(request.url, "http://localhost:8989");
    console.log("pathname", pathname);//  /login
    console.log("searchParams", searchParams); // URLSearchParams { 'username' => 'a', 'pwd' => 'b' }
    
    //获取get请求输入的文本框内容搜索参数
    const username = searchParams.get("username");
    // 获取get请求输入的密码框内容搜索参数
    const pwd = searchParams.get("pwd"); 

    console.log("username pwd", username, pwd);// a b

    
    //body参数
    request.on("data", (buffer) => {
        console.log("请求体数据",buffer.toString());// username pwd null null
    })
    

    //设置响应状态码
    response.statusCode = 201; //200--299都代表成功

    //设置响应头
    response.setHeader("Content-Type", "text/html;charset=utf-8");
    
    //设置响应体
    response.end("<h1 style='color:red;'>我是响应标题!!!</h1>");
})
server.listen("8989", () => {
    console.log("启动服务: http://localhost:8989");
});